package com.haredot.controller;

import com.haredot.entity.Btn;
import com.haredot.service.BtnService;
import com.haredot.utils.ValidUtils;
import com.haredot.vo.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/btn")
public class BtnController {

    @Resource
    private BtnService btnService ;

    @GetMapping("/list/{funcId}")
    @PreAuthorize("hasAnyAuthority('func', 'func:query')")
    public R<Btn> list(@PathVariable Long funcId) { 

        return btnService.queryBtnsByFunc(funcId);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('func:add:btn')")
    public R<?> addBtn(@Validated @RequestBody Btn btn, BindingResult bindingResult) {

        return ValidUtils.toResult(bindingResult, ()-> btnService.saveBtn(btn));
    }

    @PutMapping("/update/{btnId}")
    @PreAuthorize("hasAuthority('func:btn:update')")
    public R<?> updateBtn(@PathVariable("btnId") Long btnId, @Validated @RequestBody Btn btn, BindingResult bindingResult) {
        btn.setId(btnId);
        return ValidUtils.toResult(bindingResult, ()-> btnService.updateBtn(btn));
    }

    @DeleteMapping("/del/{btnId}")
    @PreAuthorize("hasAuthority('func:btn:delete')")
    public R<?> removeBtn(@PathVariable("btnId") Long btnId) {

        return btnService.removeBtn(btnId);
    }
}
